package homework01;

import java.util.Random;

public class Homework1Solver {


    //1.编写程序求值
    //思路1:穷举法
    //for版

    public String calculateGeometricSeriesSum() {
        int sum1 = 0;
        int number1 = 1;
        for (int j = 0; j < 20; j++) {
            number1 *= 2;
            sum1 += number1;
        }
        sum1 -= 20;
        return "" + sum1;
    }


    //while版
        /*
        int sum1 = 0;
        int cnt = 0;
        int number1 = 1;
        while(cnt <20){
            number1 *= 2;
            sum1 +=number1;
            cnt++;
        }
        sum1 -=20;
        System.out.println("结果是:" + sum1);

         */

    //do-while版
        /*
        int sum1 = 0;
        int loop = 0;
        int numer1 = 1;
        do {
            numer1 *= 2;
            sum1 += numer1;
            loop++;
        } while (loop < 20);
        sum1 -= 20;
        System.out.println("结果是:" + sum1);

         */

    //思路2:等比公式法
        /*
        int head = 2;  //首项
        int q = 2;  //公比
        double tail = Math.pow(2, 20);
        System.out.println(((head * (1 - tail)) / (1.0 - q)) - 20);

         */

    //3.编写程序，计算没有数码9的三位数有多少个，它们的和等于多少？
    //思路1：穷举法.
    //具体实现:遍历所有三位数，再剔除含九的三位数

    public void countAndSumThreeDigitsWithoutNine() {
        int count1 = 0;
        int number2 = 0;
        int sum2 = 0;
        for (int a = 1; a < 10; a++) {
            for (int b = 0; b < 10; b++) {
                for (int c = 0; c < 10; c++) {
                    if (a != 9 && b != 9 && c != 9) {
                        sum2 += (a * 100 + b * 10 + c);
                        count1 += 1;
                    }
                }
            }
        }
        System.out.println("个数为" + count1 + "\t " + "求和的值为:" + sum2);  //个数
    }


    //思路2:...


    //2.求值
    //思路1：穷举法：
    //具体实现：分奇偶，求倒数

    //while循环版

    public void calculateHarmonicSeries() {
        int n = 1;  //初项
        int cnt = 0;  //计数器
        double sum3 = 0;
        while (true) {
            if ((cnt % 2) == 0) { //保证第一次是正数
                sum3 += (1.0 / n);
                if (((1.0 / n) < 0.00001) && n > 1) {
                    System.out.println(sum3);
                    break;
                }
                cnt++;
                n += 1;
            } else {
                sum3 -= (1.0 / n);
                if (((1.0 / n) < 0.00001) && n > 1) {
                    System.out.println(sum3);
                    break;
                }
                cnt++;
                n += 1;
            }
        }
    }


    //for循环版
        /*
        int number1 = 1; //初始值
        double sum2 = 0;
        for(int i = 0; i < 100001;i++){
            if((i % 2) == 1){ //奇数
                sum2 -= (1.0 / number1);
                number1++;
            } else {
                sum2 += (1.0 / number1);
                number1++;
            }
        }
        System.out.println(sum2);

         */

    //do-while版
        /*
        int cnt2 = 0; //奇偶
        int number2 = 1; //初项
        double sum1 = 0;
        do{
            if((cnt2 % 2) == 0){
                sum1 += (1.0 / number2);
                number2++;
                cnt2++;
            } else {
                sum1 -= (1.0 / number2);
                number2++;
                cnt2++;
            }
        }while(cnt2 < 100001);
        System.out.println(sum1);

         */


    //4. section,class,subclass,main_group,subgroup
        /*
        思路1：1.生成随机数对象，用Scanner接收这些对象 -->思路二:用随机数生成
             2.判断两个特殊的地方:
             class 取值 1 的时候需要在前面补零
             main_group 取值 1 的时候需要补三个空格
             3.设置字符串拼接这些东西
             4.检查是否满足编程规范
        */

    //思路1：用户输入
        /*
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入section部分(A-H)");
        String section = scanner.next();

        System.out.println("请输入class_部分(1-99)");
        int n1 = scanner.nextInt();
        String class_ = "";
        if (n1 < 10) {
            String str1 = "0" + n1;
            class_ = str1;
        }

        System.out.println("请输入subclass_部分(A-Z)");
        String subclass_ = scanner.next();

        System.out.println("请输入main_group部分(1-9999)");
        int main_group_number = scanner.nextInt();
        String main_group = "";
        if ((main_group_number < 1000) && (main_group_number >= 100)) {
            String str2 = " " + main_group_number;
            main_group = str2;
        } else if ((main_group_number < 100) && (main_group_number >= 10)) {
            String str2 = "  " + main_group_number;
            main_group = str2;
        } else if ((main_group_number < 10) && (main_group_number >= 0)) {
            String str2 = "   " + main_group_number;
            main_group = str2;
        }

        System.out.println("请输入sub_group部分(0-99999)");
        int sub_group = scanner.nextInt();

        System.out.println("IPC形式为");
        System.out.println(section + class_ + subclass_ + main_group + "/" + sub_group);
        */


    //思路2：高级版(random,强制转换,nextInt(),)

    public void generateRandomIpcCode() {
        Random r = new Random();
        r.nextInt();
        int a = r.nextInt(65, 72);
        char section = (char) a;

        int b = r.nextInt(1, 99);
        String class_ = "";
        if (b < 10) {
            String str = "0" + b;
            class_ = str;
        } else {
            String str = "" + b;
            class_ = str;
        }

        int c = r.nextInt(65, 90);
        char subclass_ = (char) c;

        int n1 = r.nextInt(0, 9999);
        String main_group = "";
        if ((n1 < 1000) && (n1 >= 100)) {
            String str2 = " " + n1;
            main_group = str2;
        } else if ((n1 < 100) && (n1 >= 10)) {
            String str2 = "  " + n1;
            main_group = str2;
        } else if ((n1 < 10) && (n1 >= 0)) {
            String str2 = "   " + n1;
            main_group = str2;
        } else {
            String str2 = "" + n1;
            main_group = str2;
        }

        int d = r.nextInt(0, 99999);
        int subgroup = d;

        System.out.println("IPC形式为:" + "\t" + section + class_ + subclass_ + main_group + "/" + subgroup);


    }


}
